package com.tpyule.tpclass.web.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.bo.AnaBo;
import com.tpyule.tpclass.entity.bo.CourseLearnDetailBo;
import com.tpyule.tpclass.entity.bo.StudyCountBo;
import com.tpyule.tpclass.entity.bo.StudyDetailBo;
import com.tpyule.tpclass.entity.dto.CourseStudyCountDto;
import com.tpyule.tpclass.entity.dto.StudyAnaDto;
import com.tpyule.tpclass.entity.dto.StudyProDto;
import com.tpyule.tpclass.entity.dto.StudyQueryDto;
import com.tpyule.tpclass.entity.pojo.Course;
import com.tpyule.tpclass.entity.pojo.Study;
import com.tpyule.tpclass.service.CourseService;
import com.tpyule.tpclass.service.StudyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/4/14 0:09
 */
@RestController
@RequestMapping("/study")
public class StudyController extends BaseController {
    @Autowired
    StudyService studyService;
    @Autowired
    CourseService courseService;

    @RequestMapping("/getStudyDetail")
    @ResponseBody
    public void getStudyDetail(HttpServletRequest request, HttpServletResponse response) {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String queryData=request.getParameter("queryData");
            StudyQueryDto studyQueryDto =objectMapper.readValue(queryData, StudyQueryDto.class);
            if(studyQueryDto.getCourseId()==null){
                resultBean = new FailResultBean("课程ID不规范");
            }else {
                if(studyQueryDto.getUserId()==null){
                    resultBean = new FailResultBean("用户ID不规范");
                }else {
                    StudyDetailBo studyDetailBo = studyService.getStudyDetail(studyQueryDto);
                    resultBean = new SuccessResultBean<StudyDetailBo>(studyDetailBo,"获取成功");
                }
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            try {
                this.setAjaxResponseResult(response, resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/addStudy")
    @ResponseBody
    public void addStudy(HttpServletRequest request,HttpServletResponse response){
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String studyData=request.getParameter("studyData");
            String sectionId = request.getParameter("sectionId");
            Study study =objectMapper.readValue(studyData, Study.class);
            courseService.addNumber(study.getCourseId());
            int result = studyService.addStudy(study,sectionId);
            if(result>0){
                resultBean = new SuccessResultBean(study, "添加成功！");
            }else {
                resultBean = new FailResultBean("添加失败");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            try {
                this.setAjaxResponseResult(response, resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/getStudyCourse")
    @ResponseBody
    public void getStudyCourse(HttpServletRequest request, HttpServletResponse response) {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String userId=request.getParameter("userId");
            if(userId==null){
                resultBean = new FailResultBean("数据错误");
            }else {
                List<CourseLearnDetailBo> bos = studyService.getListStudyCourse(userId);
                if(bos==null){
                    resultBean = new FailResultBean("失败");
                }else {
                    resultBean = new SuccessResultBean(bos,"获取成功");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                this.setAjaxResponseResult(response, resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/getCountStudy")
    @ResponseBody
    public void getCountStudy(HttpServletRequest request, HttpServletResponse response) {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String queryData=request.getParameter("queryData");
            if(queryData==null){
                resultBean = new FailResultBean("数据错误");
            }else {
                CourseStudyCountDto dto = objectMapper.readValue(queryData, CourseStudyCountDto.class);
                if (dto!=null){
                    int result = studyService.getCountStudy(dto);
                    resultBean = new SuccessResultBean(result,"获取成功");
                }else{
                    resultBean = new FailResultBean("失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                this.setAjaxResponseResult(response, resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/getLastCountStudy")
    @ResponseBody
    public void getLastCountStudy(HttpServletRequest request, HttpServletResponse response) {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String queryData=request.getParameter("queryData");
            if(queryData==null){
                resultBean = new FailResultBean("数据错误");
            }else {
                CourseStudyCountDto dto = objectMapper.readValue(queryData, CourseStudyCountDto.class);
                if (dto!=null){
                    int result = studyService.getLastCountStudy(dto);
                    resultBean = new SuccessResultBean(result,"获取成功");
                }else{
                    resultBean = new FailResultBean("失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                this.setAjaxResponseResult(response, resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @RequestMapping("/getAnaStudy")
    @ResponseBody
    public void getAnaStudy(HttpServletRequest request,HttpServletResponse response){
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String queryStr=request.getParameter("queryData");
            StudyAnaDto dto =objectMapper.readValue(queryStr, StudyAnaDto.class);
            if (dto!=null) {
                List<AnaBo> bos = studyService.getAnaStudy(dto);
                if(bos!=null){
                    resultBean = new SuccessResultBean(bos, "成功！");
                }else {
                    resultBean = new FailResultBean("失败");
                }
            }else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            try {
                this.setAjaxResponseResult(response, resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/getAnaSex")
    @ResponseBody
    public void getAnaSex(HttpServletRequest request,HttpServletResponse response){
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String courseId=request.getParameter("courseId");
            if (courseId!=null) {
                List<StudyCountBo> bos = studyService.getCountSexStudy(courseId);
                if(bos!=null){
                    resultBean = new SuccessResultBean(bos, "成功！");
                }else {
                    resultBean = new FailResultBean("失败");
                }
            }else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                this.setAjaxResponseResult(response, resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @RequestMapping("/getAnaPro")
    @ResponseBody
    public void getAnaPro(HttpServletRequest request,HttpServletResponse response){
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String courseId=request.getParameter("courseId");
            if (courseId!=null) {
                StudyProDto dto = new StudyProDto();
                dto.setCourseId(courseId);
                List<StudyCountBo> bos = studyService.getCountProgressStudy(dto);
                if(bos!=null){
                    resultBean = new SuccessResultBean(bos, "成功！");
                }else {
                    resultBean = new FailResultBean("失败");
                }
            }else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                this.setAjaxResponseResult(response, resultBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
